package com.wzt.lianfirecontrol.adapter.usercenter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;

public class UserCenterAdapter extends BaseRecyclerAdapter<BannerModel> {

    private Context context;

    public UserCenterAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_user_center_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, BannerModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv_img;
        private TextView tv_title;
        private TextView tv_vice_title;
        private TextView tv_user_center_num;
        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.iv_img = itemView.findViewById(R.id.iv_img);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.tv_vice_title = itemView.findViewById(R.id.tv_vice_title);
        myViewHolder.tv_user_center_num = itemView.findViewById(R.id.tv_user_center_num);
    }

    private void setItemData(MyViewHolder viewHolder, BannerModel bannerModel) {
        if (StringUtils.isEmpty(bannerModel.getImg())){
            if (0 != bannerModel.getImgResource()){
                viewHolder.iv_img.setVisibility(View.VISIBLE);
                viewHolder.iv_img.setImageResource(bannerModel.getImgResource());
            }else {
                viewHolder.iv_img.setVisibility(View.GONE);
            }
        }else{
            viewHolder.iv_img.setVisibility(View.VISIBLE);
            GildeUtils.loadImage(context, viewHolder.iv_img, bannerModel.getImg());
        }
        viewHolder.tv_title.setText(bannerModel.getTitle());
        if (StringUtils.isEmpty(bannerModel.getViceTitle())){
            viewHolder.tv_vice_title.setVisibility(View.GONE);
        }else {
            viewHolder.tv_vice_title.setVisibility(View.VISIBLE);
            viewHolder.tv_vice_title.setText(bannerModel.getViceTitle());
        }
        if (StringUtils.isEmpty(bannerModel.getFlag())){
            viewHolder.tv_user_center_num.setVisibility(View.GONE);
        }else {
            viewHolder.tv_user_center_num.setVisibility(View.VISIBLE);
            viewHolder.tv_user_center_num.setText(bannerModel.getFlag());
        }
    }
}
